package com.wchat.network.message;

import java.util.ArrayList;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

import com.wchat.network.device.Device;
import com.wchat.network.device.Group;

@Root
public class WhoAmIMessage extends Message {

	private static final long serialVersionUID = -3563249873880811644L;
	
	@Element
	private Device device;
	
	@ElementList
	private ArrayList<Group> groups = new ArrayList<Group>();
	
	public WhoAmIMessage() {
		super();
	}
	
	public WhoAmIMessage(Device device) {
		super();
		this.device = device;
	}

	public Device getDevice() {
		return device;
	}

	@Override
	public byte getType() {
		return Message.TYPE_WHOAMI;
	}
	
	public synchronized void addGroup(Group group) {
		/* check id */
		long id = group.getGroupid();
		int size = groups.size();
		for (int i = 0; i < size; i++) {
			Group g = groups.get(i);
			if (id == g.getGroupid())
				return;
		}
		groups.add(group);
		bytes = Message.getMessageBytes(this);
	}
	
	public synchronized void removeGroup(Group group) {
		if (groups.contains(group)) {
			groups.remove(group);
			bytes = Message.getMessageBytes(this);
		}
	}
	
	@Override
	public String toString() {
		return "WhoAmIMessage [device=" + device + ", from=" + from + "]";
	}

	public ArrayList<Group> getGroups() {
		return groups;
	}

}
